//
//  Team9workVc.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/17.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9workVc.h"
#import "Team9ExpertFootView.h"
#import "Team9Header.h"
#import "Team9JQHeader.h"
#import "Team9ExpertIdeabookModel.h"
#import "Team9WorkHeaderView.h"
#import "Team9ExpertPro_serviceModel.h"
#import "Team9ExpertUser_home_infoModel.h"
#import "Team9ExpertIdeabookColletionCell.h"
#import "Team9WorkReviewVIew.h"
#import "Team9WorkReviewsModel.h"
#import "Team9WorkDetailView.h"
#import "Team9DesignVC.h"
#import "Team9ExpertTalkViewController.h"
#import  <GPUImage.h>

@interface Team9workVc ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) Team9WorkHeaderView *headView; // ?
@property(nonatomic, strong) UITableView *tableView;
//@property(nonatomic, strong) NSArray *array;// d

@property(nonatomic, strong) UIScrollView *darkView;
@property(nonatomic, strong) Team9WorkDetailView *detailView;//?
@property(nonatomic, strong) NSArray *topicArray;
@property(nonatomic, strong) NSArray *ideabookArray;

//@property(nonatomic, copy) NSString *workTotal; // d
@property(nonatomic, copy) NSString *topicTotal;
@property(nonatomic, copy) NSString *ideabookToal;

@property(nonatomic, strong) Team9ExpertFootView *footView;
@property(nonatomic, strong) Team9WorkReviewVIew *reviewView;

@property(nonatomic, strong) Team9WorkReviewsModel *reviewModel;
@property(nonatomic, strong) UIImageView *startImage;
@property(nonatomic, retain) GPUImageGaussianBlurFilter *blurFilter;

@end

@implementation Team9workVc


- (void)startAnimation {
    
    self.startImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    
    self.startImage.image = [self convertViewToImage];
    
    self.blurFilter = [[GPUImageGaussianBlurFilter alloc] init];
    self.blurFilter.blurRadiusInPixels = 0;
    self.startImage.image =
    [self.blurFilter imageByFilteringImage:self.startImage.image];
    
    // 讲设置好的UIVisualEffectView添加到要模糊的视图层上
    
    [self.view addSubview:self.startImage];
    [self shopAnimation];
}

- (void)shopAnimation {
    
    [UIView animateWithDuration:0.6
                     animations:^{
                         self.startImage.alpha = 0;
                         
                     }completion:^(BOOL finished) {
                         [self.startImage removeFromSuperview];
                     }] ;
    [UIView animateWithDuration:0.0001
                     animations:^{
                         [UIView setAnimationDelay:0.2];
                         self.blurFilter.blurRadiusInPixels = 5;
                         self.startImage.image = [self.blurFilter
                                                  imageByFilteringImage:self.startImage.image];
                     }];
}
-(void)updateBlur
{
    self.startImage.image = [self convertViewToImage];
}
-(UIImage *)convertViewToImage
{
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@", _url);

    

    self.headView = [[Team9WorkHeaderView alloc] initWithType:ViewTYpeLight];
    self.headView.frame = CGRectMake(0, 0, WIDTH, 120 + 300 * WIDTH / 375);
    self.headView.userId = self.Id;
//    self.headView.ratings = self.ratings;
    __weak __typeof(self)weakSelf = self;
    self.headView.block = ^{
        [weakSelf startAnimation];
        [weakSelf.darkView setHidden:NO];
    };
//        [self.view addSubview:_headView];
    

    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, WIDTH, HEGHT + 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
//    self.tableView.bounces = NO;
    
    Team9BaseView *backView = [[Team9BaseView alloc] initWithType:ViewTYpeLight];
    backView.frame = CGRectMake(0, 0, WIDTH, 64);
    [self.view addSubview:backView];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    backButton.frame = CGRectMake(0, 20, 60, 44);
    [backView addSubview:backButton];
    if ([Team9EveningType shareEvening].eveningFailling) {
        [backButton setImage:[UIImage imageNamed:@"back2"] forState:UIControlStateNormal];
    } else [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    

    self.footView = [[Team9ExpertFootView alloc] init];
    self.footView.frame = CGRectMake(0, 0, WIDTH, 40 * 3 * WIDTH / 375);
    
    self.footView.Id = _Id;
    self.footView.vc = self;
    self.tableView.contentInset = UIEdgeInsetsMake(64 + 49, 0, 0, 0);
    self.empty = [Team9EmptyTool toolWithScrollView:self.tableView]
    ;
    [self createDarkView];
    [self createData];
    
}
- (void)reload
{
    [self.reviewView removeFromSuperview];
    self.reviewView = nil;
    CGRect frame = self.headView.frame;
    frame.size.height = 120 + 300 * WIDTH / 375;
    self.headView.frame = frame;
    self.reviewView = [[Team9WorkReviewVIew alloc] init];
    [self.headView addSubview:self.reviewView];
    CGFloat height = 100 * WIDTH/ 375 + 70 + [Team9Tool heightByFloatSize:12 * WIDTH / 375 With:self.reviewModel.content];
    self.reviewView.frame = CGRectMake(0, self.headView.frame.size.height + 10, WIDTH, height);
    frame.size.height = height + 10 + frame.size.height;
    self.headView.frame = frame;
//    self.tableView.contentInset = UIEdgeInsetsMake(self.headView.frame.size.height + 20 + 44 + self.reviewView.frame.size.height + 10, 0, 0, 0);
//    [self.tableView setContentOffset:CGPointMake(0, frame.origin.y - 64)];
    __weak __typeof(self)weakSelf = self;
    [self.reviewView setBlock:^{
        weakSelf.hidesBottomBarWhenPushed = YES;
        Team9ExpertTalkViewController *talkVC = [Team9ExpertTalkViewController new];
        talkVC.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/user_home_review_list?user_id=%@", weakSelf.Id];
        [weakSelf.navigationController pushViewController:talkVC animated:YES];
    }];
}
- (void)createDarkView{
    //    self.darkView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.darkView = [UIScrollView new];
    self.darkView.bounces = NO;
    [self.view addSubview:_darkView];
    [_darkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(20, 0, 0, 0));
    }];
    UIView *view = [UIView new];
    [self.darkView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.8f;

    
    self.detailView = [[Team9WorkDetailView alloc] init];
    [self.darkView addSubview:_detailView];
    
    [_detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.darkView).priorityLow();
        make.width.equalTo(@(WIDTH));
        make.top.mas_greaterThanOrEqualTo(self.darkView);
        make.bottom.mas_lessThanOrEqualTo(self.darkView);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"X" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.darkView addSubview:button];
    [button addTarget:self action:@selector(hideDark) forControlEvents:UIControlEventTouchUpInside];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-49 / 2);
        make.width.height.equalTo(@49);
        make.centerX.equalTo(self.view);
    }];
//    [self hideDark];
    [self.darkView setHidden:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}
- (void)hideDark{
    [self startAnimation];
    [self.darkView setHidden:YES];
}
//- (void)setArray:(NSArray *)array
//{
//    if (_array != array) {
//        _array = array;
//        [self.tableView reloadData];
//    }
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 && _ideabookArray.count) {
        return 1;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && _ideabookArray.count) {
        CGFloat n = (self.ideabookArray.count + 1) / 2;
        return WIDTH / 2 * (int)n + 30 * WIDTH / 375 + 10;
    }
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0 && _ideabookArray.count) {
        return [NSString stringWithFormat:@"灵感画册 %@", self.ideabookToal];
    }
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *reuse = @"IdeabookColletionCellReuse";
        Team9ExpertIdeabookColletionCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9ExpertIdeabookColletionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        cell.Id = _Id;
        cell.vc = self;
        __weak __typeof(self)weakSelf = self;
        [cell setBlock:^(NSInteger index) {
            weakSelf.navigationController.navigationBar.hidden = NO;
            Team9ExpertIdeabookModel *model = weakSelf.ideabookArray[index];
            Team9DesignVC *vc = [Team9DesignVC new];
            vc.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page2?col_id=%@&tab=photo", model.col_id];
            weakSelf.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        cell.array = self.ideabookArray;
        return cell;
    }
    return nil;
    
}
- (void)createData{
//    NSLog(@"%@", _url);
    [Team9RefreshTool getUrl:_url success:^(id result) {
//        self.tableView.contentInset = UIEdgeInsetsMake(20 + self.headView.frame.size.height + 44, 0, 0, 0);

        //        self.workTotal = result[@"data"][@"user_home_work_list"][@"total"];
        self.topicTotal = result[@"data"][@"user_home_topic_list"][@"topic_total"];
        self.ideabookToal = result[@"data"][@"user_home_ideabook_list"][@"total"];
        self.ideabookArray = [Team9ExpertIdeabookModel baseModelByArr:result[@"data"][@"user_home_ideabook_list"][@"collections"]];
        
        [self.headView.serviceModel setValuesForKeysWithDictionary: result[@"data"][@"user_home_info"][@"pro_service"]];
        [self.headView.userModel setValuesForKeysWithDictionary: result[@"data"][@"user_home_info"][@"user_info"]];
        self.headView.pro_ratings = result[@"data"][@"user_home_info"][@"pro_ratings"];
        [self.headView reload];
        
        self.detailView.string = self.headView.userModel.nick;
        self.detailView.model = self.headView.serviceModel;
        self.footView.model = self.headView.userModel;
        [self.darkView setContentOffset:CGPointZero];
        NSArray *array = result[@"data"][@"user_home_review_list"][@"reviews"];
        if (array.count) {
            //            NSLog(@"%@", self.reviewView.reviewcount);
            self.reviewModel = [Team9WorkReviewsModel new];
            [self.reviewModel setValuesForKeysWithDictionary:array[0]];
            [self reload];
            self.reviewView.reviewcount = result[@"data"][@"user_home_review_list"][@"total"];
            self.reviewView.model = self.reviewModel;
        }
        self.tableView.tableFooterView = _footView;
        self.tableView.tableHeaderView = _headView;
        [self.tableView reloadData];

    } scrollView:self.tableView empty:self.empty];
}
- (void)pop{
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController.navigationBar setHidden:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
